//
//  ReasonView.m
//  GAMCAfterSales
//
//  Created by BigAmazing on 2018/7/22.
//  Copyright © 2018年 glendale. All rights reserved.
//



#import "ReasonView.h"
#import "popContentViewController.h"
#import "DDPopoverBackgroundView.h"
#import "MainMenuDataModel.h"
//RGB颜色
#define RGBColor(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@interface ReasonView ()<ContentViewControllerDelegate>{
    //下拉菜单属性
    UIPopoverController *popCon;
}
@property(nonatomic,strong)popContentViewController *contentCon;


@property(nonatomic,strong)UILabel * activeText;
@property(nonatomic,strong)UIButton * reasonBtn;
@property(nonatomic,strong)UITextField * otherTextfield;

@property(nonatomic,strong)UIButton * canelBtn;
@property(nonatomic,strong)UIButton * makeSureBtn;

@property(nonatomic,strong)NSString * reasonStr;
@end
@implementation ReasonView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self==[super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [UIColor grayColor].CGColor;
    
    [self setupTitle];
    [self contentV];
}
//设置内容
-(void)contentV{
    UILabel * activeTitle = [self createLabelWithframe:CGRectMake(30, 60, 100, 20) text:@"活动名称"];
    [self addSubview:activeTitle];
    
    _activeText = [self createLabelWithframe:CGRectMake(150, 60, 200, 20) text:@""];
    [self addSubview:_activeText];
    
    UILabel * reasonTitle =[self createLabelWithframe:CGRectMake(30, 100, 100, 20) text:@"不参加原因"];
    [self addSubview:reasonTitle];
    
    _reasonBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _reasonBtn.frame = CGRectMake(150, 95, 200, 30);
    [_reasonBtn setTitle:@"" forState:UIControlStateNormal];
    [_reasonBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_reasonBtn setBackgroundImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
    [_reasonBtn addTarget:self action:@selector(reasonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_reasonBtn];
    
    
    UILabel * otherReasonTitle =[self createLabelWithframe:CGRectMake(30, 140, 100, 20) text:@"其他原因"];
    [self addSubview:otherReasonTitle];
    _otherTextfield = [[UITextField alloc]initWithFrame:CGRectMake(150, 135, 200, 30)];
    _otherTextfield.font = [UIFont systemFontOfSize:14];
    _otherTextfield.layer.borderWidth = 0.5;
    _otherTextfield.layer.borderColor = [UIColor grayColor].CGColor;
    _otherTextfield.enabled = NO;
    [self addSubview:_otherTextfield];
    
  
    _canelBtn = [self creatBtnWithFrame:CGRectMake(180, 235, 100, 45) Title:@"取消" action:@selector(cannelClick)];
    [self addSubview:_canelBtn];
    
    _makeSureBtn = [self creatBtnWithFrame:CGRectMake(320, 235, 100, 45) Title:@"确定" action:@selector(makeSureClick)];
    [self addSubview:_makeSureBtn];
}
-(void)makeSureClick{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (self.reasonStr.length <= 0) {
        UIAlertView * aler =[[UIAlertView alloc]initWithTitle:nil message:@"请选择不参加服务活动原因" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil
                             ];
        [aler show];
        return;
        
    }
    if ([self.reasonStr isEqualToString:@"90501005"] && self.otherTextfield.text.length <= 0) {
        
        UIAlertView * aler =[[UIAlertView alloc]initWithTitle:nil message:@"请填写不参加服务活动的其他原因" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        return;
    }
 
    NSDictionary *baseInfoDic = modelIns.dicBaseMessage;
    NSMutableDictionary * reasonDic = [[NSMutableDictionary alloc]init];
    reasonDic[@"VIN"] = baseInfoDic[@"VIN"];
    reasonDic[@"ASC_NAME"] = @"";
    [reasonDic setValue:self.dataDic[@"ACTIVITY_CODE"] forKey:@"ACTIVITY_CODE"];//活动代码
    [reasonDic setValue:self.dataDic[@"ACTIVITY_NAME"] forKey:@"ACTIVITY_NAME"];
    [reasonDic setValue:@"A" forKey:@"UPDATE_STATUS"];
    [reasonDic setValue:self.reasonStr forKey:@"MAIN_REASON_CODE"];
    [reasonDic setValue:self.reasonBtn.titleLabel.text forKey:@"MAIN_REASON"];
    [reasonDic setValue:self.otherTextfield.text forKey:@"OTHER_REASON"];
    [modelIns.activeReasonArr addObject:reasonDic];
    
    NSMutableArray * reapairArr  =[[NSMutableArray alloc]init];
   reapairArr =  modelIns.repairProjects;
    NSArray * tmpReapair = [NSArray arrayWithArray:reapairArr];
    
    for (NSDictionary * dic in tmpReapair) {
        if ([dic[@"ACTIVITY_CODE"] isEqualToString:_dataDic[@"ACTIVITY_CODE"]]) {
            [reapairArr removeObject:dic];
        }
    }
    modelIns.repairProjects = reapairArr;
    
    NSMutableArray * partArr =[[NSMutableArray alloc]init];
   partArr =  modelIns.partList;
    NSArray * tmpPart =[NSArray arrayWithArray:partArr];
    for (NSDictionary * dic in tmpPart) {
        if ([dic[@"ACTIVITY_CODE"] isEqualToString:_dataDic[@"ACTIVITY_CODE"]]) {
            [partArr removeObject:dic];
        }
    }
    modelIns.partList = partArr;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(ReasonViewClickMakeSureBtn)]) {
        [self.delegate ReasonViewClickMakeSureBtn];
    }
}
-(void)cannelClick{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(ReasonViewClickCanelBtn)]) {
        [self.delegate ReasonViewClickCanelBtn];
    }
}
-(void)setupTitle{
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.mj_w, 30)];
    title.text = @"不参加活动原因";
    title.font = [UIFont boldSystemFontOfSize:20.f];
    title.textAlignment =  NSTextAlignmentCenter;
    [self addSubview:title];
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(title.frame)+10, self.mj_w-40, 2)];
    line.backgroundColor = [UIColor redColor];
    [self addSubview:line];
}

-(void)reasonClick:(UIButton *)sender{
    //创建下拉菜单
    _contentCon.list = nil;
    [self createThePopViewControl:sender];
    NSArray *source = self.reasonNameArr;
    _contentCon.list = source;
    [_contentCon.myTableView reloadData];
    
}
//创建下拉菜单方法
-(void)createThePopViewControl:(UIButton *)sender
{
    if (!_contentCon)
    {
        _contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
        _contentCon.delegate = self;
    }
    
    _contentCon.btnSender=sender;
    
    popCon = [[UIPopoverController alloc] initWithContentViewController:_contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:222 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    
    [popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [popCon setPopoverContentSize:CGSizeMake(sender.frame.size.width, 200)];
    
    [popCon presentPopoverFromRect:CGRectMake(40, 10, 70, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    
}
#pragma mark ---- 弹出的回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
  
    NSString *key = nil;

        key = self.reasonNameArr[row];
        self.reasonStr = self.reasonCodeArr[row];

    if ([self.reasonStr isEqualToString:@"90501005"]) {
        self.otherTextfield.enabled = YES;
    }else{
        self.otherTextfield.enabled = NO;
        self.otherTextfield.text = @"";

    }
    
    [btn setTitle:key forState:UIControlStateNormal];
    
    //清空数据，退出控件
    [popCon dismissPopoverAnimated:YES];
    popCon = nil;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.activeText.text = dataDic[@"LABOUR_NAME"];
    
}

-(UILabel *)createLabelWithframe:(CGRect)frame text:(NSString *)text{
    UILabel *lbl = [[UILabel alloc]initWithFrame:frame];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text = text;
    lbl.font = [UIFont systemFontOfSize:14];
    lbl.backgroundColor = RGBColor(202,202,202);
    
    return lbl;
}

-(UIButton *)creatBtnWithFrame:(CGRect)frame Title:(NSString *)title action:(SEL)action{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"ht05-1"] forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}


@end
